
package com.xebest.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xebest.common.enums.util.EnumUtil;
import com.xebest.web.form.XeVenAreaRoleForm;
import com.xebest.web.form.XeVenStoreRoleForm;
import com.xebest.web.model.XeVenAreaRoleEntity;
import com.xebest.web.model.XeVenStoreRoleEntity;
import com.xebest.web.view.ErpUserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 设置中心实体类转换
 *
 * @author 李贝贝
 */
public class SysSetBeanUtils extends BeanUtils {

    /**
     * 仓库角色权限实体转换
     *
     * @author 李贝贝
     */
    public static XeVenStoreRoleEntity toStoreRoleEntity(XeVenStoreRoleForm form) {
        XeVenStoreRoleEntity entity = new XeVenStoreRoleEntity();
        copyProperties(form, entity);
        return entity;
    }

    /**
     * 转换要保存的角色仓库权限
     *
     * @author 李贝贝
     */
    public static List<XeVenStoreRoleEntity> toStoreRoleList(XeVenStoreRoleForm form) {
        String[] storeRoleArr = form.getStoreIdArr();
        List<XeVenStoreRoleEntity> paramEntites = new ArrayList<XeVenStoreRoleEntity>();
        for (int i = 0; i < storeRoleArr.length; i++) {
            XeVenStoreRoleEntity entity2 = SysSetBeanUtils.toStoreRoleEntity(form);
            entity2.setStoreId(storeRoleArr[i].toString());
            paramEntites.add(entity2);
        }
        return paramEntites;
    }

    /**
     * 区域角色权限实体转换
     *
     * @author 李贝贝
     */
    public static XeVenAreaRoleEntity toAreaRoleEntity(XeVenAreaRoleForm form) {
        XeVenAreaRoleEntity entity = new XeVenAreaRoleEntity();
        copyProperties(form, entity);
        return entity;
    }

    /**
     * 转换要保存的角色区域权限
     *
     * @author 李贝贝
     */
    public static List<XeVenAreaRoleEntity> toAreaRoleList(XeVenAreaRoleForm form) {
        String[] storeRoleArr = form.getAreaIdArr();
        List<XeVenAreaRoleEntity> paramEntites = new ArrayList<XeVenAreaRoleEntity>();
        for (int i = 0; i < storeRoleArr.length; i++) {
            XeVenAreaRoleEntity entity2 = SysSetBeanUtils.toAreaRoleEntity(form);
            entity2.setAreaId(Integer.valueOf(storeRoleArr[i]));
            paramEntites.add(entity2);
        }
        return paramEntites;
    }

    /**
     * 获取ERP用户登录信息
     *
     * @author 李贝贝
     */
    public static ErpUserInfo getErpUserInfo(JSONObject object) {
        ErpUserInfo userInfo = null;
        if (null == object)
            return userInfo;
        userInfo = JSON.parseObject(object.toJSONString(), ErpUserInfo.class);
        return userInfo;
    }

    /**
     * 数字字典转string
     *
     * @author 赵盼飞
     */
    @SuppressWarnings("rawtypes")
    public static <T, E extends Enum> void codeReversionStr(String attributeName, T t, Class<E> en) {
        try {
            Method method = null;
            PropertyDescriptor descriptor = new PropertyDescriptor(attributeName, t.getClass());
            method = descriptor.getReadMethod();
            String value = (String) method.invoke(t, null);
            if (!StringUtils.isEmpty(value)) {
                String strVal = EnumUtil.getEnumNameByValue(en, value);
                method = descriptor.getWriteMethod();
                method.invoke(t, strVal);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  
